﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Management
{
    public partial class DetailedView : System.Web.UI.Page
    {
        public Dao dao;
        protected void Page_Load(object sender, EventArgs e)
        {
            dao=new Dao();
            if (Request.Form["__EVENTTARGET"] == "removeGoal")
            {
                RemoveGoal(Request.Form["__EVENTARGUMENT"]);
            }
            else if (Request.Form["__EVENTTARGET"] == "removeTask")
            {
                RemoveTask(Request.Form["__EVENTARGUMENT"]);
            }
            else if (Request.Form["__EVENTTARGET"] == "changeWeekData")
            {
                ChangeWeekData(Request.Form["__EVENTARGUMENT"]);
            }
            else if (Request.Form["__EVENTTARGET"] == "changeStatus")
            {
                ChangeStatus(Request.Form["__EVENTARGUMENT"]);
            }
        }

        private void ChangeStatus(string task)
        {
            try
            {
                var items = task.Split('~');
                dao.ChangeTaskStatus(items[1], items[0]);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void ShowException(string exceptionTxt)
        {
            ScriptManager.RegisterStartupScript(this, GetType(),
            "showExceptionPop", "showExceptionPop('" + exceptionTxt + "');", true);
        }

        private void RemoveTask(string data)
        {
            try
            {
                var goal = data.Split('~')[0];
                var task = data.Split('~')[1];
                dao.RemoveTask(task, goal);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void RemoveGoal(string goal)
        {
            try
            {
                dao.RemoveGoal(goal);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }

        private void ChangeWeekData(string weekData)
        {
            var error = false;
            var items = weekData.Split('@');
            for(var i=0;i<items.Length-1;i++)
            {
                var weekInfo = items[i].Split('-');
                var time = weekInfo[2].Split('~');
                if (time[1] == "null")
                    time[1] = null;
                try
                {
                    dao.SaveWeekTimeInfo(weekInfo[0], Int32.Parse(weekInfo[1]), Int32.Parse(time[0]), time[1]);
                }
                catch (Exception)
                {
                    error = true;
                }
            }
            if (error)
            {
                ShowException("Exception in time saving!!!");
            }
        }

        protected void addTaskBtn_OnClick(object sender, EventArgs e)
        {
            try
            {
                dao.AddTask(goalNameTask.Text, newTaskName.Text, newTAskDescription.Text);
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }
    }
}